import React, { Component } from 'react';
import { connect } from 'dva';
import { Modal, Form, Input, InputNumber,TreeSelect } from 'antd';
const TreeNode = TreeSelect.TreeNode;
const FormItem = Form.Item;

class UpdateApplyModal extends Component {
  constructor(props) {
    super(props);
    this.state = {
      visible: false,
    };
  }

  showModelHandler = (e) => {
    if (e) e.stopPropagation();
    this.props.form.resetFields();
    this.setState({
      visible: true,
    });
  };

  hideModelHandler = () => {
    this.setState({
      visible: false,
    });
  };

  okHandler = () => {
    const { onOk } = this.props;
    this.props.form.validateFields((err, values) => {
      if (!err) {
        onOk(values);
        this.hideModelHandler();
      }
    });
  };

  render() {
    const { children,assetType,  title } = this.props;
    const { getFieldDecorator } = this.props.form;
    const { count,typeId_,desc} = this.props.record;
    const formItemLayout = {
      labelCol: { span: 6 },
      wrapperCol: { span: 14 },
    };
    const loop = data => data.map((item) => {
      if (item.assetType && item.assetType.length) {
        return <TreeNode key={item.id_+''} title={item.name} value={item.id_+''}>{loop(item.assetType)}</TreeNode>;
      }
      return <TreeNode key={item.id_+''} title={item.name} value={item.id_+''} />;
    });


    return (
        <span>
            <span onClick={this.showModelHandler}>
                { children }
            </span>
            <Modal
                confirmLoading={false}
                title={title}
                visible={this.state.visible}
                onOk={this.okHandler.bind(this)}
                onCancel={this.hideModelHandler}
            >
              <Form layout='horizontal' onSubmit={this.okHandler.bind(this)}>
               
                <FormItem
                  {...formItemLayout}
                  hasFeedback
                  label="资产分类"
                >
                  {
                    getFieldDecorator('typeId', {
                      initialValue: typeof(typeId_)=="undefined" ? undefined : typeId_+'',
                      rules: [{
                        required: true, message: '请选择资产分类',
                      }]
                    })(
                      <TreeSelect
                        showSearch
                        treeNodeFilterProp="title"
                        dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
                        placeholder="请选择资产分类"
                        notFoundContent="无匹配结果"
                        allowClear
                        treeDefaultExpandAll
                      >
                         { assetType.assetType && loop(assetType.assetType) }
                      </TreeSelect>
                    )
                  }
                </FormItem>                           
                <FormItem
                  {...formItemLayout}
                  label="备注"
                  hasFeedback
                >
                  {
                    getFieldDecorator('desc', {
                      initialValue: desc,
                      rules: [
                        { type: 'string', max: 200, message: '最多200个字符' }
                      ],
                    })(<Input type="textarea" rows={5} />)
                  }
                </FormItem>                    
              </Form>
            </Modal> 
        </span>
    );
  }
}

export default Form.create()(UpdateApplyModal);